package com.woniuxy.merchant.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.commons.util.DateTimeUtil;
import com.woniu.commons.util.WebUtil;
import com.woniu.dto.NoticeListDto;
import com.woniu.form.PublishNoticeForm;
import com.woniu.form.QueryNoticeForm;
import com.woniu.form.UpdateNoticeForm;
import com.woniu.zsd.commons.core.exception.ZsdException;
import com.woniu.zsd.commons.core.model.Result;
import com.woniu.zsd.merchant.client.PermissionClient;
import com.woniu.zsd.merchant.form.Permission;
import com.woniuxy.merchant.entity.Notis;
import com.woniuxy.merchant.mapper.NotisMapper;
import com.woniuxy.merchant.service.INotisService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-05-13
 */
@Service
public class NotisServiceImpl extends ServiceImpl<NotisMapper, Notis> implements INotisService {
    @Resource
    private NotisMapper notisMapper;
    @Resource
    private PermissionClient permissionClient;

    @Override
    public void publishNotice(PublishNoticeForm publishNoticeForm, Integer adminId) {
        // 远程调用查询用户信息
        Result<Permission> info = permissionClient.selInfoByToken(WebUtil.getRequest().getHeader("token"));
        System.out.println(info);
        //通过id查询姓名
        String name = info.getData().getName();
        Notis notis = new Notis();
        notis.setTitle(publishNoticeForm.getTitle());
        notis.setContent(publishNoticeForm.getContent());
        notis.setPublishUserId(adminId);
        notis.setPublishUserName(name);
        notis.setUpdateUserId(adminId);
        notis.setUpdateUserName(name);
        notisMapper.insert(notis);

    }

    @Override
    public Page<NoticeListDto> noticeList(QueryNoticeForm queryNoticeForm) {
        Page<Notis> page = new Page<>(
                queryNoticeForm.getPageNum() == null ? 1 : queryNoticeForm.getPageNum(),
                queryNoticeForm.getPageSize() == null ? 5 : queryNoticeForm.getPageSize()
        );
        QueryWrapper<Notis> wrapper = new QueryWrapper<>();

        // 删除状态为1（未删除）
        wrapper.eq("is_delete", 1);

        // 模糊搜索标题
        if (StrUtil.isNotBlank(queryNoticeForm.getTitle())) {
            wrapper.like("title", queryNoticeForm.getTitle());
        }

        // 时间范围
        if (StrUtil.isNotBlank(queryNoticeForm.getStartTime())) {
            wrapper.ge("creat_time", DateTimeUtil.stringToDateTime(queryNoticeForm.getStartTime()));
        }
        if (StrUtil.isNotBlank(queryNoticeForm.getEndTime())) {
            wrapper.le("creat_time", DateTimeUtil.stringToDateTime(queryNoticeForm.getEndTime()));
        }
        Page<Notis> result = notisMapper.selectPage(page, wrapper);
        List<NoticeListDto> dtos = BeanUtil.copyToList(result.getRecords(), NoticeListDto.class);
        Page<NoticeListDto> pages = new Page<>();
        BeanUtil.copyProperties(result, pages);
        return pages;
    }

    @Override
    public boolean deleteNotice(Integer noticeId) {
        //远程调用产看用户信息
        Result<Permission> result = permissionClient.selInfoByToken(WebUtil.getRequest().getHeader("token"));

        //通过用户信息获取用户ID
        Integer id = Integer.parseInt(String.valueOf(result.getData().getId()));
        //通过id查询出姓名
        String adminName = result.getData().getName();
        //根据noticeId查询出对应的公告
        Notis notice = notisMapper.selectById(noticeId);
        if (notice == null && notice.getIsDelete()!=1)throw new ZsdException("公告不存在",500);
        notice.setIsDelete(0);
        notice.setUpdateUserId(id);
        notice.setUpdateUserName(adminName);
        //修改时间是这位当前时间
        notice.setUpdateTime(LocalDateTime.now());
        notisMapper.updateById(notice);
        return true;
    }

    @Override
    public boolean updateNotice(UpdateNoticeForm updateNoticeForm) {
        // 远程查看用户信息
        Result<Permission> userInfo = permissionClient.selInfoByToken(WebUtil.getRequest().getHeader("token"));
        //通过用户信息获取用户ID
        Integer id = Integer.parseInt(String.valueOf(userInfo.getData().getId()));
        //通过id查询出姓名
        String name = userInfo.getData().getName();
        //根据noticeId查询出对应的公告
        Notis notice = notisMapper.selectById(updateNoticeForm.getId());
        if (notice == null && notice.getIsDelete()!=1)throw new ZsdException("公告不存在",500);
        notice.setTitle(updateNoticeForm.getTitle());
        notice.setContent(updateNoticeForm.getContent());
        notice.setUpdateUserId(id);
        notice.setUpdateTime(LocalDateTime.now());
        notice.setUpdateUserName(name);
        notisMapper.updateById(notice);
        return true ;
    }
}
